package com.kaiyidiannao.model.req.model;

import android.text.TextUtils;
import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.PlayBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *   玩乐住, 美食街,石子街 公用的请求数据-搜索
 */
public class GetPlaySearch extends BaseCommand {

    private int mid; //当前分类编号
    private String title; //搜索关键词
//    private int page = DataCenter.LISTVIEW_DEFAULT_PAGE;
    private String geotable_id;
    private String ak;
    private String location;
    private String radius;
    private String q;
    private String page;
    private String TAG = "GetPlaySearch";
    private String sortby;
//    public GetPlaySearch(int catid, String title) {
//        this.catid = catid;
//        this.title = title;
//    }

    public GetPlaySearch(int mid,String ak,String geotable_id, String location,String radius,String q,String page,String sortby) {
        this.mid = mid;
        this.geotable_id = geotable_id;
        this.location = location;
        this.radius = radius;
        this.ak = ak;
        this.page = page;
        this.q = q;
        this.sortby = sortby;
    }

    public GetPlaySearch(String ak,String geotable_id, String location,String radius,String q,String page,int mid) {
        this.geotable_id = geotable_id;
        this.location = location;
        this.radius = radius;
        this.ak = ak;
        this.page = page;
        this.q = q;
        this.mid = mid;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_BAIDU_ADDRESS;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        if(mid!=0){
            paramters.put("filter", "mid:"+mid+","+mid);
        }
        paramters.put("page_index",page+"");
        paramters.put("q",q);
        paramters.put("ak",ak);
        paramters.put("geotable_id",geotable_id);
        paramters.put("radius",radius);
        paramters.put("location",location);
        if(!TextUtils.isEmpty(sortby)){
            paramters.put("sortby",sortby);
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetPlayLIstTop.yes()");

        try {
            Log.d(TAG, result.object.toString());
            JSONObject jsonObject = new JSONObject(result.object.toString());
            JSONArray  jsonArray = jsonObject.optJSONArray("contents");
            ArrayList<PlayBean> targetList = DataCenter.playBeanList;
            if (Integer.parseInt(page) == DataCenter.LISTVIEW_BAIDU_DEFAULT_PAGE) {
                targetList.clear();
            }

            for (int i = 0;i<jsonArray.length();i++){
                JSONObject o = (JSONObject) jsonArray.get(i);
                PlayBean bean = new PlayBean();
                bean.setSize(jsonArray.length()+"");
//                JSONObject oj = o.getJSONObject("thumb");
                bean.setThumb(o.getString("thumb"));
                bean.setStar(o.getString("star"));
                bean.setShopLongitude(o.optJSONArray("location").get(0)+"");
                bean.setShopLatitude(o.optJSONArray("location").get(1)+"");
                bean.setTitle(o.getString("title"));
                bean.setAddress(o.getString("address"));
                bean.setDistance(o.getString("distance"));
                bean.setWeight(o.getString("weight"));
                bean.setItemid(o.getString("itemid"));
                bean.setCatid(o.getString("catid"));
                targetList.add(bean);
            }
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_PLAY_LIST_SUCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_PLAY_LIST_SUCESS);
    }
}
